package com.jingxing.controller;

import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import com.jingxing.pojo.MatchInfo;
import com.jingxing.service.MatchInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/matchInfo")
@Api(value = "API", tags = {"比赛相关"})
@UserLoginToken
public class MatchInfoController {
    @Autowired
    private MatchInfoService matchInfoService;


    @PostMapping("/getMatchInfos")
    @ApiOperation(value = "查询某个阶段比赛信息")
    public ApiResult getMatchInfos(Integer competitionLevel, String name, String phone){
        if (competitionLevel == null) {
            return ApiResult.parameterError("比赛阶段不能为空");
        }
        List<MatchInfo> list = matchInfoService.getMatchInfos(competitionLevel, name, phone);
        return ApiResult.ok(list);
    }

    @PostMapping("/lateTimes")
    @ApiOperation("录入选手迟到次数")
    public ApiResult lateTimes(String id, Integer lateFrequency, String updateBy) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        if (lateFrequency == null) {
            return ApiResult.parameterError("迟到次数不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        MatchInfo matchInfo = new MatchInfo();
        matchInfo.setId(id);
        matchInfo.setLateFrequency(lateFrequency);
        matchInfo.setUpdateBy(updateBy);
        boolean updateById = matchInfoService.updateById(matchInfo);
        return ApiResult.result(updateById);
    }

    @PostMapping("/score")
    @ApiOperation("选手打分")
    public ApiResult score(String id, Integer rating, String updateBy) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        if (rating == null) {
            return ApiResult.parameterError("分数不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        MatchInfo matchInfo = new MatchInfo();
        matchInfo.setId(id);
        matchInfo.setRating(rating);
        matchInfo.setUpdateBy(updateBy);
        boolean updateById = matchInfoService.updateById(matchInfo);
        return ApiResult.result(updateById);
    }

    @PostMapping("/promotionPlayer")
    @ApiOperation("选手晋级/淘汰")
    @Transactional
    public ApiResult promotionPlayer(String id, Integer promotionStatus, String updateBy) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        if (promotionStatus == null) {
            return ApiResult.parameterError("晋级状态不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        MatchInfo matchInfo = new MatchInfo();
        matchInfo.setId(id);
        matchInfo.setPromotionStatus(promotionStatus);
        matchInfo.setUpdateBy(updateBy);
        boolean updateById = matchInfoService.updateById(matchInfo);
        if (updateById && promotionStatus == 1) {
            MatchInfo byId = matchInfoService.getById(id);
            MatchInfo info = new MatchInfo();
            info.setPlayerId(byId.getPlayerId());
            info.setCreateBy(updateBy);
            if (byId.getCompetitionLevel() == 9) {
                info.setCompetitionLevel(0);
            }
            else if (byId.getCompetitionLevel() == 0) {
                info.setCompetitionLevel(1);
            }
            else if (byId.getCompetitionLevel() == 1) {
                info.setCompetitionLevel(2);
            }
            else if (byId.getCompetitionLevel() == 2) {
                info.setCompetitionLevel(3);
            }
            boolean save = matchInfoService.save(info);
            return ApiResult.result(save);
        }
        return ApiResult.result(updateById);
    }

    @PostMapping("/promotionPlayers")
    @ApiOperation("选手批量晋级/淘汰")
    @Transactional
    public ApiResult promotionPlayers(List<String> ids, Integer promotionStatus, String updateBy) {
        if (ids == null || ids.size() == 0) {
            return ApiResult.parameterError("IDS不能为空");
        }
        if (promotionStatus == null) {
            return ApiResult.parameterError("晋级状态不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        List<MatchInfo> matchInfos = new ArrayList<>();
        for (String id: ids) {
            MatchInfo matchInfo = new MatchInfo();
            matchInfo.setId(id);
            matchInfo.setPromotionStatus(promotionStatus);
            matchInfo.setUpdateBy(updateBy);
            matchInfos.add(matchInfo);
        }
        boolean b = matchInfoService.updateBatchById(matchInfos);
        if (b && promotionStatus == 1) {
            List<MatchInfo> matchInfoList = new ArrayList<>();
            for (String id: ids) {
                MatchInfo byId = matchInfoService.getById(id);
                MatchInfo info = new MatchInfo();
                info.setPlayerId(byId.getPlayerId());
                info.setUpdateBy(updateBy);
                info.setPromotionStatus(promotionStatus);
                if (byId.getCompetitionLevel() == 9) {
                    info.setCompetitionLevel(0);
                }
                else if (byId.getCompetitionLevel() == 0) {
                    info.setCompetitionLevel(1);
                }
                else if (byId.getCompetitionLevel() == 1) {
                    info.setCompetitionLevel(2);
                }
                else if (byId.getCompetitionLevel() == 2) {
                    info.setCompetitionLevel(3);
                }
                matchInfoList.add(info);
            }
            boolean saveBatch = matchInfoService.saveBatch(matchInfoList);
            return ApiResult.result(saveBatch);
        }
        return ApiResult.result(b);
    }


}
